package com.machineapps.agchefia;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AGChefiaConfig extends Activity {

	private EditText alcoolText;
	
	private EditText gasolinaText;
	
	private float percent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agchefia_config);
		// Show the Up button in the action bar.
		this.alcoolText = (EditText) findViewById(R.id.alcool_text);

		this.gasolinaText = (EditText) findViewById(R.id.gasolina_text);

		Button calcularButton = (Button) findViewById(R.id.calcular_button);
	    calcularButton.setOnClickListener(new CalcularListener());
		
		SharedPreferences sharedPref = getSharedPreferences(AGChefiaConstants.STORED_PERCENT,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putFloat(AGChefiaConstants.STORED_PERCENT, percent);
		editor.commit();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			return true;
	}
	
	class CalcularListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			float precoAlcool = Float.parseFloat(alcoolText.getText().toString());
			float precoGasolina = Float.parseFloat(gasolinaText.getText().toString());
			percent = 0f;
			if(precoAlcool != 0 && precoGasolina != 0){
				percent = precoAlcool/precoGasolina;
			}
			SharedPreferences sharedPref = getSharedPreferences(AGChefiaConstants.STORED_PERCENT,Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = sharedPref.edit();
			editor.putFloat(AGChefiaConstants.STORED_PERCENT, percent);
			editor.commit();
			
			}
		}
	}

